package cn.fivestars.springcloud.gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * config
 *
 * @author liangchenzhou1024@163.com
 * @date 2021/3/8
 */
@Configuration
public class GatewayConfig {

    //@Bean
    //public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
    //    RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
    //    routes.route("cloud-payment-service",
    //            route -> route.path("/guonei").uri("http://news.baidu.com/guonei"));
    //    return routes.build();
    //}

     @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("cloud-payment-service",
                route -> route
                        // cookie : name, predicate
                        .path("/cloud-payment-service/**")
                        .and()
                        .after(ZonedDateTime.parse("2021-03-10 21:31:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"))))
                        //.and()
                        //.cookie("username", "zzyy")
                        //.and()
                        //.header("X-Request-Id", "\\d+")
                        .uri("lb://cloud-payment-service")
                        //.filter(new AddRequestParameterGatewayFilterFactory().apply("X-Request-Id", new AbstractNameValueGatewayFilterFactory.NameValueConfig()
                        //        .setName("id")
                        //        .setValue("1024")))
        );
        return routes.build();
    }
}
